﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using log4net;
using RealState.BLL;
using RealState.BusinessObjects;
using System.IO;
using RealState.Utils;

namespace RealState.W.AdminControl
{
    public partial class ImagesEdit : System.Web.UI.UserControl
    {
        protected ILog log = LogManager.GetLogger(typeof(ImagesCreate));
        private ImagesBLL m_ImagesBLL;
        private ImageTypesBLL m_ImageTypesBLL;
        private Images m_ImagesDO;

        protected void Button2_Click(object sender, EventArgs e)
        {
            bindThum();
        }

        private void LoadType()
        {
            m_ImageTypesBLL = new ImageTypesBLL();
            try
            {
                drpImageTypes.DataSource = m_ImageTypesBLL.GetImageTypess();
                drpImageTypes.DataTextField = "Tile";
                drpImageTypes.DataValueField = "Id";
                drpImageTypes.DataBind();
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            finally
            {
                m_ImageTypesBLL = null;
            }
        }

        private void bindThum()
        {
            string path = string.Empty;
            ddlThumbnail.Items.Clear();
            if (hdImages.Value == "1")
                path = "~/upload/Logo/";
            if (hdImages.Value == string.Empty)
                path = "~/upload/Logo/";
            if (hdImages.Value == "2")
                path = "~/upload/Banners/";
            if (hdImages.Value == "3")
                path = "~/upload/Ad/";
            if (hdImages.Value == "4")
                path = "~/upload/Partner/";
            string[] files = Directory.GetFiles(Server.MapPath(path));
            Action<string[], bool> action = delegate(string[] filde, bool forThumbs)
            {
                foreach (string str in files)
                {
                    string str5 = Path.GetExtension(str).ToLower();
                    string item = Path.GetFileName(str);
                    switch (str5)
                    {
                        case ".jpg":
                        case ".jpeg":
                        case ".png":
                        case ".gif":
                            if (forThumbs)
                            {
                                this.ddlThumbnail.Items.Add(item);
                            }
                            break;
                    }
                }
            };
            action(files, true);
        }

        protected void btnBindImagesThum_Click(object sender, EventArgs e)
        {
            string text = string.Empty;
            if (ddlThumbnail.SelectedIndex != -1)
            {
                text = ddlThumbnail.SelectedItem.Text.Trim();
            }
            bindThum();
            if (Session["uploaded_pic_thum"] != null)
            {
                string str3 = Session["uploaded_pic_thum"].ToString().Trim();
                Session["uploaded_pic_thum"] = null;
                if (Session["uploaded_pic_ext"] != null)
                {
                    if (Session["uploaded_pic_ext"].ToString().Trim().ToLower() == "thumb")
                    {
                        if (ddlThumbnail.Items.FindByText(str3) != null)
                        {
                            ddlThumbnail.SelectedIndex = -1;
                            ddlThumbnail.Items.FindByText(str3).Selected = true;
                        }

                    }

                    Session["uploaded_pic_ext"] = null;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
                {
                    m_ImagesBLL = new ImagesBLL();
                    m_ImagesDO = new Images();
                    try
                    {
                        m_ImagesDO = m_ImagesBLL.GetImages(Int32.Parse(Request.QueryString["ID"]));
                        txtQuantity.Text = m_ImagesDO.Sorted.ToString();
                        txtTitle.Text = m_ImagesDO.Title;
                        txtTitleAlias.Text = m_ImagesDO.ClickUrl;
                        LoadType();
                        drpImageTypes.SelectedValue = m_ImagesDO.ImgType.ToString();
                        hdImages.Value = m_ImagesDO.ImgType.ToString() ;
                        bindThum();
                        ddlThumbnail.SelectedValue = m_ImagesDO.ImgPath.ToString();
                        if (m_ImagesDO.IsActive == true)
                            cbkActice.Checked = true;

                    }
                    catch (Exception ex)
                    {
                        log.Error(ex.Message);
                    }
                    finally {
                        m_ImagesDO = null;
                        m_ImagesBLL = null;
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            m_ImagesBLL = new ImagesBLL();
            m_ImagesDO = new Images();
            try
            {
                m_ImagesDO.Id = Int32.Parse(Request.QueryString["ID"]);
                m_ImagesDO.Title = txtTitle.Text;
                m_ImagesDO.Sorted = Int32.Parse(txtQuantity.Text);
                m_ImagesDO.ImgPath = ddlThumbnail.SelectedValue;
                m_ImagesDO.ImgType = Int32.Parse(drpImageTypes.SelectedValue);
                m_ImagesDO.ClickUrl = txtTitleAlias.Text;
                if (cbkActice.Checked)
                    m_ImagesDO.IsActive = true;
                else
                    m_ImagesDO.IsActive = false;

                int result = m_ImagesBLL.UpdateImages(m_ImagesDO);
                if (result > 0)
                    JavaScript.Success("Cập nhật thành công", Page, Request.Url.ToString());
                else
                    JavaScript.Error("Có lỗi xảy ra ", Page);
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
            finally
            {
                m_ImagesDO = null;
                m_ImagesBLL = null;
            }
        }
    }
}